package com.cn.jm.util;

import com.cn.jm.core.tool.JMToolString;
import org.jsoup.helper.StringUtil;
import sun.misc.BASE64Decoder;

import javax.servlet.ServletInputStream;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author
 * @description 压缩工具类
 * @date 2020/3/19
 */
public class GZipUtil {

    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

    /**
     * 功能描述: 解压
     * @author
     * @date 2020/3/19
     * @param inputStream
     * @return java.lang.String
     */
    public static String ungzipString(ServletInputStream inputStream) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        if (StringUtil.isBlank(sb.toString())) {
            return null;
        }
        ByteArrayOutputStream outputStream = null;
        ByteArrayInputStream in = null;
        GZIPInputStream gin = null;
        String decompressed = null;
        try {
            byte[] compressed = new BASE64Decoder().decodeBuffer(sb.toString());
            outputStream = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            gin = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = gin.read(buffer)) != -1) {
                outputStream.write(buffer, 0, offset);
            }
            decompressed = outputStream.toString();
        } catch (IOException e) {
            e.printStackTrace();
            decompressed = null;
        } finally {
            if (gin != null) {
                try {
                    gin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return decompressed;
        }
    }


    /**
     * 功能描述: 压缩
     * @author
     * @date 2020/3/19
     * @param str
     * @return byte[]
     */
    public static byte[] compress(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(GZIP_ENCODE_UTF_8));
            gzip.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }
}
